package sso

import (
	"gitee.com/krio/ginc/example/core/lib/conf"
	"github.com/click33/sa-token-go/core"
	"github.com/click33/sa-token-go/core/adapter"
	"github.com/click33/sa-token-go/storage/memory"
	"github.com/click33/sa-token-go/storage/redis"
	"github.com/click33/sa-token-go/stputil"
)

// InitSSO 初始化sso
func InitSSO() error {
	var (
		storage adapter.Storage
		err     error
	)
	if conf.C.Login.Host == "" {
		storage = memory.NewStorage()
	} else {
		storage, err = redis.NewStorageFromConfig(&redis.Config{
			Host:     conf.C.Login.Host,
			Port:     conf.C.Login.Port,
			Password: conf.C.Login.Password,
			Database: conf.C.Login.DB,
		})
		if err != nil {
			return err
		}
	}
	stputil.SetManager(
		core.NewBuilder().
			Storage(storage).                         // 存储方式
			TokenName("Authorization").               // 名称
			Timeout(int64(conf.C.Login.ExpiresTime)). // 缓存时长
			TokenStyle(core.TokenStyleTik).           // Token风格
			IsShare(true).                            // 是否共享Token
			IsPrintBanner(false).                     // 显示启动Banner
			Build(),
	)
	return err
}
